package Com.Hupochuan.Uil.Util;

import java.sql.Timestamp;

import Com.Hupochuan.Dal.Util.TimeUtil;

/*
 * 转换时间为友善字符串的类
 */
public class TimeStringUtil {
	/*
	 * 单一实例
	 */
	private TimeStringUtil(){
		//重置系统时差
		this.resetSystemTimeDiff();
	}
	private static TimeStringUtil timeStringUtil = new TimeStringUtil();
	public static TimeStringUtil getObject(){
		return timeStringUtil;
	}
	
	//系统时差
	private long systemTimeDiff;
	
	/*
	 * 重置系统时差
	 */
	private void resetSystemTimeDiff(){
		systemTimeDiff = System.currentTimeMillis() - TimeUtil.getObject().findDBNowTime().getTime();
	}
	
	/*
	 * 固定参数 
	 * 一分钟 一小时 一天 365.24天(即一年) 的秒数
	 */
	private int minuteSeconds = 60;
	private int hourSeconds = 3600; 
	private int daySeconds = 86400;
	private int yearSeconds = 31556736;
	
	/*
	 * 将时间转换成字符串
	 */
	public String timeToTimeString(Timestamp paraTime){
		
		//创建返回字符串缓冲容器
		StringBuffer timeBuff = new StringBuffer();
		
		//获得当前时间毫秒数
		long nowTimeMillis = System.currentTimeMillis() - systemTimeDiff;
		
		//获得参数时间毫秒数
		long paraTimeMillis = paraTime.getTime();
		
		//获得当前时间与参数时间的时差(秒数)
		long paraTimeDiff = (nowTimeMillis - paraTimeMillis)/1000;
		
		//如果小于一分钟
		if(paraTimeDiff < minuteSeconds){
			timeBuff.append("刚刚");
		}
		//如果小于一小时
		else if(paraTimeDiff < hourSeconds){
			timeBuff.append(paraTimeDiff / minuteSeconds);
			timeBuff.append("分钟前");
		}
		//如果小于一天
		else if(paraTimeDiff < daySeconds){
			timeBuff.append(paraTimeDiff / hourSeconds);
			timeBuff.append("小时前");
		}
		//如果小于365.24天
		else if(paraTimeDiff < yearSeconds){
			timeBuff.append(paraTimeDiff / daySeconds);
			timeBuff.append("天前");
		}
		//大于一年
		else{
			timeBuff.append(paraTimeDiff / yearSeconds);
			timeBuff.append("年前");
		}
		
		//返回值
		return timeBuff.toString();
	}
}
